
from sanic import Blueprint

from controllers.indicator import IndicatorController
from decorators import response_json
from utils.parser import parse_args

bp = Blueprint("indicator", url_prefix="/indicator")


@bp.route("", methods=["GET", "POST"])
@response_json
async def indicator(request):
    """指标列表"""
    if request.method == "GET":
        return IndicatorController.get_indicators()
    elif request.method == "POST":
        args_rules = [
            dict(arg="abbr", required=True, type="str"),
            dict(arg="name", required=False, type="str"),
            dict(arg="name_en", required=False, type="str"),
            dict(arg="instruction", required=False, type="str"),
            dict(arg="direction", required=False, type="str"),
            dict(arg="star", required=False, type="int"),
        ]
        args = parse_args(request, "json", args_rules)
        IndicatorController.create(**args)


@bp.route("/<id:int>", methods=["GET", "DELETE", "PUT"])
@response_json
async def indicator_id(request, id):
    """ 指标的单个操作
    """
    indicator_ctl = IndicatorController(id)
    if request.method == "GET":
        return indicator_ctl.indicator
    elif request.method == "DELETE":
        indicator_ctl.delete()
        return {}
    elif request.method == "PUT":
        args_rules = [
            dict(arg="abbr", required=True, type="str"),
            dict(arg="name", required=False, type="str"),
            dict(arg="name_en", required=False, type="str"),
            dict(arg="instruction", required=False, type="str"),
            dict(arg="direction", required=False, type="str"),
            dict(arg="star", required=False, type="int"),
        ]
        args = parse_args(request, "json", args_rules)
        indicator_ctl.update(**args)
        return {}

